package com.stormragetech.teamwork.module.user.spring.redis.impl;

import com.stormragetech.common.dao.RedisBaseDao;
import com.stormragetech.common.exception.StormragetechException;
import com.stormragetech.teamwork.common.constant.Constants;
import com.stormragetech.teamwork.module.user.spring.redis.RedisDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import redis.clients.jedis.JedisPool;

/**
 * description: //TODO
 *
 * @author: Xue Bin
 * @version: 1.0.0-SNAPSHOT
 */
@Repository
public class RedisDaoImpl extends RedisBaseDao implements RedisDao {

    @Autowired
    public RedisDaoImpl(JedisPool pool) {
        super(pool);
    }

    @Override
    public void setKV(String key, String value) throws StormragetechException {
        try {
            set(key, value);
        } catch (Exception e) {
            throw new StormragetechException(Constants.REDIS_ERR_CODE, e.getMessage(), e);
        }
    }

    @Override
    public void setKV(String key, String value, int seconds) throws StormragetechException {

        try {
            if (seconds < 1) {
                setKV(key, value);
            } else {
                setex(key, value, seconds);
            }
        } catch (Exception e) {
            throw new StormragetechException(Constants.REDIS_ERR_CODE, e.getMessage(), e);
        }
    }

    @Override
    public void delKey(String key) throws StormragetechException {
        try {
            del(key);
        } catch (Exception e) {
            throw new StormragetechException(Constants.REDIS_ERR_CODE, e.getMessage(), e);
        }
    }

    @Override
    public String getKey(String key) throws StormragetechException {
        try {
            return get(key);
        } catch (Exception e) {
            throw new StormragetechException(Constants.REDIS_ERR_CODE, e.getMessage(), e);
        }
    }

}
